#include "Timer.h"

void
Timer::
tickAndWait (Uint32 timeSliceMs) {

  // Wait until "timeSlice" ms are elapsed
  // If set to zero dont wait and simply mark the timeStart
  // static variable.
  static Uint32 timeStart;
  if (timeSliceMs == 0) {
    timeStart = SDL_GetTicks();
  }
  else {
    Uint32 timeCurrent = SDL_GetTicks();
    signed long waitTimeSigned = (signed long)timeSliceMs - (signed long)timeCurrent + (signed long)timeStart;
    // Warning: a delay may not be necessary if the code block took
    // more time than expected.
    // The application will then suffer a lag.
    if (waitTimeSigned > 0) {
      SDL_Delay( (Uint32)waitTimeSigned );
    }
  }
}

